Skip to contentSkip to conversation

Change in openvpn[master]: test_networking: Allow to test --netns

Openvpn-devel flichtenheld (Code Review) 1 participant 3 messages Mar 25 1 message not in the archive

lore
Share & tools
lore

A message that is not in the archive (sent off-list or not yet received): gerrit.1774440822000.Ibaf84cba0124320f7c49e28d1b3364dac9e8b6d9@...2715...

#1 lore raw

Attention is currently required from: plaisthos.

Hello plaisthos,

I'd like you to do a code review.
Please visit

    http://gerrit.openvpn.net/c/openvpn/+/1586?usp=email

to review the following change.

Change subject: test_networking: Allow to test --netns
......................................................................

test_networking: Allow to test --netns

While here also do some cleanups:
- Run under set -eu to better catch errors
- Some comment improvments
- Fix argument handling in test_networking, it
actually ignored the interface argument

Change-Id Ibaf84cba0124320f7c49e28d1b3364dac9e8b6d9

---
M tests/t_net.sh
M tests/unit_tests/openvpn/test_networking.c

2 files changed, 57 insertions(+), 34 deletions(-)

git pull ssh://gerrit.openvpn.net:29418/openvpn refs/changes/86/1586/1

tests/t_net.sh +36 -22
index 8134832..d2c87ec 100755
@@ -1,8 +1,14 @@
 #!/usr/bin/env bash
+# Requires bash due to array handling
 
-IFACE="ovpn-dummy0"
+set -eu
+
+IFACE="${IFACE:-ovpn-dummy0}"
+NETNS=${NETNS:-}
 UNIT_TEST="./unit_tests/openvpn/networking_testdriver"
+# Tests 0 to LAST_AUTO_TEST output a ip command to compare results
 LAST_AUTO_TEST=7
+# Tests after LAST_AUTO_TEST are only executed, no comparison
 LAST_TEST=8
 
 srcdir="${srcdir:-.}"
@@ -12,33 +18,19 @@
 
 # bail out right away on non-linux. NetLink (the object of this test) is only
 # used on Linux, therefore testing other platform is not needed.
-#
-# Note: statements in the rest of the script may not even pass syntax check on
-# solaris/bsd. It uses /bin/bash
 if [ "$(uname -s)" != "Linux" ]; then
     echo "$0: this test runs only on Linux. SKIPPING TEST."
     exit 77
 fi
 
-# Commands used to retrieve the network state.
-# State is retrieved after running sitnl and after running
-# iproute commands. The two are then compared and expected to be equal.
-typeset -a GET_STATE
-GET_STATE[0]="ip link show dev $IFACE | sed 's/^[0-9]\+: //'"
-GET_STATE[1]="ip addr show dev $IFACE | sed 's/^[0-9]\+: //'"
-GET_STATE[2]="ip route show dev $IFACE"
-GET_STATE[3]="ip -6 route show dev $IFACE"
-
-LAST_STATE=$((${#GET_STATE[@]} - 1))
-
 reload_dummy()
 {
-    $RUN_SUDO ip link del $IFACE
-    $RUN_SUDO ip link add $IFACE address 00:11:22:33:44:55 type dummy
-    $RUN_SUDO ip link set dev $IFACE state up
+    $RUN_SUDO $IP_EXEC ip link del $IFACE || true
+    $RUN_SUDO $IP_EXEC ip link add $IFACE address 00:11:22:33:44:55 type dummy
+    $RUN_SUDO $IP_EXEC ip link set dev $IFACE state up
 
     if [ $? -ne 0 ]; then
-        echo "can't create interface $IFACE"
+        echo "can't create interface $IFACE $IP_NETNS"
         exit 1
     fi
 }
@@ -51,7 +43,7 @@
         # the unit-test prints to stdout the iproute command corresponding
         # to the sitnl operation being executed.
         # Format is "CMD: <commandhere>"
-        OUT=$($RUN_SUDO $UNIT_TEST $k $IFACE)
+        OUT=$($RUN_SUDO $UNIT_TEST $k $IFACE $NETNS)
         # ensure unit test worked properly
         if [ $? -ne 0 ]; then
             echo "unit-test $k errored out:"
@@ -60,7 +52,7 @@
         fi
 
         NEW=$(echo "$OUT" | sed -n 's/CMD: //p')
-        CMD="$CMD $RUN_SUDO $NEW ;"
+        CMD="$CMD $IP_EXEC $NEW ;"
     done
 
     # collect state for later comparison
@@ -72,6 +64,8 @@
 
 ## execution starts here
 
+set +eu
+
 # t_client.rc required only for RUN_SUDO definition
 if [ -r "${top_builddir}"/t_client.rc ]; then
     . "${top_builddir}"/t_client.rc
@@ -79,6 +73,8 @@
     . "${srcdir}"/t_client.rc
 fi
 
+set -eu
+
 if [ ! -x "$openvpn" ]; then
     echo "no (executable) openvpn binary in current build tree. FAIL." >&2
     exit 1
@@ -129,6 +125,24 @@
     fi
 fi
 
+IP_EXEC="$RUN_SUDO"
+if [ -n "$NETNS" ]; then
+    echo "Using netns $NETNS"
+    [ -f "/run/netns/$NETNS" ] || $RUN_SUDO ip netns add "$NETNS"
+    IP_EXEC="$RUN_SUDO ip netns exec $NETNS"
+fi
+
+# Commands used to retrieve the network state.
+# State is retrieved after running sitnl and after running
+# iproute commands. The two are then compared and expected to be equal.
+typeset -a GET_STATE
+GET_STATE[0]="$IP_EXEC ip link show dev $IFACE | sed 's/^[0-9]\+: //'"
+GET_STATE[1]="$IP_EXEC ip addr show dev $IFACE | sed 's/^[0-9]\+: //'"
+GET_STATE[2]="$IP_EXEC ip route show dev $IFACE"
+GET_STATE[3]="$IP_EXEC ip -6 route show dev $IFACE"
+
+LAST_STATE=$((${#GET_STATE[@]} - 1))
+
 for i in $(seq 0 $LAST_AUTO_TEST); do
     # reload dummy module to cleanup state
     reload_dummy
@@ -167,7 +181,7 @@
 done
 
 # remove interface for good
-$RUN_SUDO ip link del $IFACE
+$IP_EXEC ip link del $IFACE
 
 for i in $(seq $(($LAST_AUTO_TEST + 1)) ${LAST_TEST}); do
     $RUN_SUDO $UNIT_TEST $i
tests/unit_tests/openvpn/test_networking.c +21 -12
index 1c9cac1..4e2cce8 100644
@@ -6,26 +6,27 @@
 #include <cmocka.h>
 
 static char *iface = "ovpn-dummy0";
+static openvpn_net_ctx_t *ctx = NULL;
 
 static int
 net__iface_up(bool up)
 {
     printf("CMD: ip link set %s %s\n", iface, up ? "up" : "down");
 
-    return net_iface_up(NULL, iface, up);
+    return net_iface_up(ctx, iface, up);
 }
 
 static int
 net__iface_new(const char *name, const char *type)
 {
-    return net_iface_new(NULL, name, type, NULL);
+    return net_iface_new(ctx, name, type, NULL);
 }
 
 static int
 net__iface_type(const char *name, const char *type)
 {
     char ret_type[IFACE_TYPE_LEN_MAX];
-    int ret = net_iface_type(NULL, name, ret_type);
+    int ret = net_iface_type(ctx, name, ret_type);
     if (ret == 0)
     {
         assert_string_equal(type, ret_type);
@@ -37,7 +38,7 @@
 static int
 net__iface_del(const char *name)
 {
-    return net_iface_del(NULL, name);
+    return net_iface_del(ctx, name);
 }
 
 static int
@@ -45,7 +46,7 @@
 {
     printf("CMD: ip link set %s mtu %d\n", iface, mtu);
 
-    return net_iface_mtu_set(NULL, iface, mtu);
+    return net_iface_mtu_set(ctx, iface, mtu);
 }
 
 static int
@@ -64,7 +65,7 @@
 
     printf("CMD: ip addr add %s/%d dev %s broadcast +\n", addr_str, prefixlen, iface);
 
-    return net_addr_v4_add(NULL, iface, &addr, prefixlen);
+    return net_addr_v4_add(ctx, iface, &addr, prefixlen);
 }
 
 static int
@@ -81,7 +82,7 @@
 
     printf("CMD: ip -6 addr add %s/%d dev %s\n", addr_str, prefixlen, iface);
 
-    return net_addr_v6_add(NULL, iface, &addr, prefixlen);
+    return net_addr_v6_add(ctx, iface, &addr, prefixlen);
 }
 
 static int
@@ -110,7 +111,7 @@
     }
     printf("\n");
 
-    return net_route_v4_add(NULL, &dst, prefixlen, NULL, iface, 0, metric);
+    return net_route_v4_add(ctx, &dst, prefixlen, NULL, iface, 0, metric);
 }
 
 static int
@@ -146,7 +147,7 @@
     }
     printf("\n");
 
-    return net_route_v4_add(NULL, &dst, prefixlen, &gw, iface, 0, metric);
+    return net_route_v4_add(ctx, &dst, prefixlen, &gw, iface, 0, metric);
 }
 
 static int
@@ -173,7 +174,7 @@
     }
     printf("\n");
 
-    return net_route_v6_add(NULL, &dst, prefixlen, NULL, iface, 0, metric);
+    return net_route_v6_add(ctx, &dst, prefixlen, NULL, iface, 0, metric);
 }
 
 static int
@@ -206,7 +207,7 @@
     }
     printf("\n");
 
-    return net_route_v6_add(NULL, &dst, prefixlen, &gw, iface, 0, metric);
+    return net_route_v6_add(ctx, &dst, prefixlen, &gw, iface, 0, metric);
 }
 
 static void
@@ -232,11 +233,19 @@
         return 0;
     }
 
-    if (argc > 3)
+    if (argc >= 3)
     {
         iface = argv[2];
     }
 
+    if (argc >= 4)
+    {
+        ctx = malloc(sizeof(*ctx));
+        CLEAR(*ctx);
+        ctx->netns = argv[3];
+        ctx->gc = gc_new();
+    }
+
     test = atoi(argv[1]);
     switch (test)
     {

--
To view, visit http://gerrit.openvpn.net/c/openvpn/+/1586?usp=email
To unsubscribe, or for help writing mail filters, visit http://gerrit.openvpn.net/settings?usp=email

Gerrit-MessageType: newchange
Gerrit-Project: openvpn
Gerrit-Branch: master
Gerrit-Change-Id: Ibaf84cba0124320f7c49e28d1b3364dac9e8b6d9
Gerrit-Change-Number: 1586
Gerrit-PatchSet: 1
Gerrit-Owner: flichtenheld <[email protected]...>
Gerrit-Reviewer: plaisthos <[email protected]...>
Gerrit-CC: openvpn-devel <[email protected]>
Gerrit-Attention: plaisthos <[email protected]...>

#2 lore raw
In reply to a message not in the archive

Attention is currently required from: flichtenheld, plaisthos.

its_Giaan has posted comments on this change by flichtenheld. ( http://gerrit.openvpn.net/c/openvpn/+/1586?usp=email )

Change subject: test_networking: Allow to test --netns
......................................................................

Patch Set 1: Code-Review+2

(1 comment)

Patchset:

PS1:
LGTM

--
To view, visit http://gerrit.openvpn.net/c/openvpn/+/1586?usp=email
To unsubscribe, or for help writing mail filters, visit http://gerrit.openvpn.net/settings?usp=email

Gerrit-MessageType: comment
Gerrit-Project: openvpn
Gerrit-Branch: master
Gerrit-Change-Id: Ibaf84cba0124320f7c49e28d1b3364dac9e8b6d9
Gerrit-Change-Number: 1586
Gerrit-PatchSet: 1
Gerrit-Owner: flichtenheld <[email protected]...>
Gerrit-Reviewer: its_Giaan <[email protected]...>
Gerrit-Reviewer: plaisthos <[email protected]...>
Gerrit-CC: openvpn-devel <[email protected]>
Gerrit-Attention: plaisthos <[email protected]...>
Gerrit-Attention: flichtenheld <[email protected]...>
Gerrit-Comment-Date: Wed, 25 Mar 2026 13:06:31 +0000
Gerrit-HasComments: Yes
Gerrit-Has-Labels: Yes

#3 lore raw
In reply to a message not in the archive

Attention is currently required from: flichtenheld, its_Giaan, plaisthos.

Hello its_Giaan, plaisthos,

I'd like you to reexamine a change. Please visit

    http://gerrit.openvpn.net/c/openvpn/+/1586?usp=email

to look at the new patch set (#2).

The following approvals got outdated and were removed:
Code-Review+2 by its_Giaan

Change subject: test_networking: Allow to test --netns
......................................................................

test_networking: Allow to test --netns

While here also do some cleanups:
- Run under set -eu to better catch errors
- Some comment improvments
- Fix argument handling in test_networking, it
actually ignored the interface argument

Change-Id Ibaf84cba0124320f7c49e28d1b3364dac9e8b6d9

---
M tests/t_net.sh
M tests/unit_tests/openvpn/test_networking.c

2 files changed, 58 insertions(+), 34 deletions(-)

git pull ssh://gerrit.openvpn.net:29418/openvpn refs/changes/86/1586/2

tests/t_net.sh +37 -22
index 8134832..e388971 100755
@@ -1,8 +1,14 @@
 #!/usr/bin/env bash
+# Requires bash due to array handling
 
-IFACE="ovpn-dummy0"
+set -eu
+
+IFACE="${IFACE:-ovpn-dummy0}"
+NETNS=${NETNS:-}
 UNIT_TEST="./unit_tests/openvpn/networking_testdriver"
+# Tests 0 to LAST_AUTO_TEST output a ip command to compare results
 LAST_AUTO_TEST=7
+# Tests after LAST_AUTO_TEST are only executed, no comparison
 LAST_TEST=8
 
 srcdir="${srcdir:-.}"
@@ -12,33 +18,19 @@
 
 # bail out right away on non-linux. NetLink (the object of this test) is only
 # used on Linux, therefore testing other platform is not needed.
-#
-# Note: statements in the rest of the script may not even pass syntax check on
-# solaris/bsd. It uses /bin/bash
 if [ "$(uname -s)" != "Linux" ]; then
     echo "$0: this test runs only on Linux. SKIPPING TEST."
     exit 77
 fi
 
-# Commands used to retrieve the network state.
-# State is retrieved after running sitnl and after running
-# iproute commands. The two are then compared and expected to be equal.
-typeset -a GET_STATE
-GET_STATE[0]="ip link show dev $IFACE | sed 's/^[0-9]\+: //'"
-GET_STATE[1]="ip addr show dev $IFACE | sed 's/^[0-9]\+: //'"
-GET_STATE[2]="ip route show dev $IFACE"
-GET_STATE[3]="ip -6 route show dev $IFACE"
-
-LAST_STATE=$((${#GET_STATE[@]} - 1))
-
 reload_dummy()
 {
-    $RUN_SUDO ip link del $IFACE
-    $RUN_SUDO ip link add $IFACE address 00:11:22:33:44:55 type dummy
-    $RUN_SUDO ip link set dev $IFACE state up
+    $RUN_SUDO $IP_EXEC ip link del $IFACE || true
+    $RUN_SUDO $IP_EXEC ip link add $IFACE address 00:11:22:33:44:55 type dummy
+    $RUN_SUDO $IP_EXEC ip link set dev $IFACE state up
 
     if [ $? -ne 0 ]; then
-        echo "can't create interface $IFACE"
+        echo "can't create interface $IFACE $IP_NETNS"
         exit 1
     fi
 }
@@ -51,7 +43,7 @@
         # the unit-test prints to stdout the iproute command corresponding
         # to the sitnl operation being executed.
         # Format is "CMD: <commandhere>"
-        OUT=$($RUN_SUDO $UNIT_TEST $k $IFACE)
+        OUT=$($RUN_SUDO $UNIT_TEST $k $IFACE $NETNS)
         # ensure unit test worked properly
         if [ $? -ne 0 ]; then
             echo "unit-test $k errored out:"
@@ -60,7 +52,7 @@
         fi
 
         NEW=$(echo "$OUT" | sed -n 's/CMD: //p')
-        CMD="$CMD $RUN_SUDO $NEW ;"
+        CMD="$CMD $IP_EXEC $NEW ;"
     done
 
     # collect state for later comparison
@@ -72,13 +64,18 @@
 
 ## execution starts here
 
+set +eu
+
 # t_client.rc required only for RUN_SUDO definition
+RUN_SUDO=
 if [ -r "${top_builddir}"/t_client.rc ]; then
     . "${top_builddir}"/t_client.rc
 elif [ -r "${srcdir}"/t_client.rc ]; then
     . "${srcdir}"/t_client.rc
 fi
 
+set -eu
+
 if [ ! -x "$openvpn" ]; then
     echo "no (executable) openvpn binary in current build tree. FAIL." >&2
     exit 1
@@ -129,6 +126,24 @@
     fi
 fi
 
+IP_EXEC="$RUN_SUDO"
+if [ -n "$NETNS" ]; then
+    echo "Using netns $NETNS"
+    [ -f "/run/netns/$NETNS" ] || $RUN_SUDO ip netns add "$NETNS"
+    IP_EXEC="$RUN_SUDO ip netns exec $NETNS"
+fi
+
+# Commands used to retrieve the network state.
+# State is retrieved after running sitnl and after running
+# iproute commands. The two are then compared and expected to be equal.
+typeset -a GET_STATE
+GET_STATE[0]="$IP_EXEC ip link show dev $IFACE | sed 's/^[0-9]\+: //'"
+GET_STATE[1]="$IP_EXEC ip addr show dev $IFACE | sed 's/^[0-9]\+: //'"
+GET_STATE[2]="$IP_EXEC ip route show dev $IFACE"
+GET_STATE[3]="$IP_EXEC ip -6 route show dev $IFACE"
+
+LAST_STATE=$((${#GET_STATE[@]} - 1))
+
 for i in $(seq 0 $LAST_AUTO_TEST); do
     # reload dummy module to cleanup state
     reload_dummy
@@ -167,7 +182,7 @@
 done
 
 # remove interface for good
-$RUN_SUDO ip link del $IFACE
+$IP_EXEC ip link del $IFACE
 
 for i in $(seq $(($LAST_AUTO_TEST + 1)) ${LAST_TEST}); do
     $RUN_SUDO $UNIT_TEST $i
tests/unit_tests/openvpn/test_networking.c +21 -12
index 1c9cac1..4e2cce8 100644
@@ -6,26 +6,27 @@
 #include <cmocka.h>
 
 static char *iface = "ovpn-dummy0";
+static openvpn_net_ctx_t *ctx = NULL;
 
 static int
 net__iface_up(bool up)
 {
     printf("CMD: ip link set %s %s\n", iface, up ? "up" : "down");
 
-    return net_iface_up(NULL, iface, up);
+    return net_iface_up(ctx, iface, up);
 }
 
 static int
 net__iface_new(const char *name, const char *type)
 {
-    return net_iface_new(NULL, name, type, NULL);
+    return net_iface_new(ctx, name, type, NULL);
 }
 
 static int
 net__iface_type(const char *name, const char *type)
 {
     char ret_type[IFACE_TYPE_LEN_MAX];
-    int ret = net_iface_type(NULL, name, ret_type);
+    int ret = net_iface_type(ctx, name, ret_type);
     if (ret == 0)
     {
         assert_string_equal(type, ret_type);
@@ -37,7 +38,7 @@
 static int
 net__iface_del(const char *name)
 {
-    return net_iface_del(NULL, name);
+    return net_iface_del(ctx, name);
 }
 
 static int
@@ -45,7 +46,7 @@
 {
     printf("CMD: ip link set %s mtu %d\n", iface, mtu);
 
-    return net_iface_mtu_set(NULL, iface, mtu);
+    return net_iface_mtu_set(ctx, iface, mtu);
 }
 
 static int
@@ -64,7 +65,7 @@
 
     printf("CMD: ip addr add %s/%d dev %s broadcast +\n", addr_str, prefixlen, iface);
 
-    return net_addr_v4_add(NULL, iface, &addr, prefixlen);
+    return net_addr_v4_add(ctx, iface, &addr, prefixlen);
 }
 
 static int
@@ -81,7 +82,7 @@
 
     printf("CMD: ip -6 addr add %s/%d dev %s\n", addr_str, prefixlen, iface);
 
-    return net_addr_v6_add(NULL, iface, &addr, prefixlen);
+    return net_addr_v6_add(ctx, iface, &addr, prefixlen);
 }
 
 static int
@@ -110,7 +111,7 @@
     }
     printf("\n");
 
-    return net_route_v4_add(NULL, &dst, prefixlen, NULL, iface, 0, metric);
+    return net_route_v4_add(ctx, &dst, prefixlen, NULL, iface, 0, metric);
 }
 
 static int
@@ -146,7 +147,7 @@
     }
     printf("\n");
 
-    return net_route_v4_add(NULL, &dst, prefixlen, &gw, iface, 0, metric);
+    return net_route_v4_add(ctx, &dst, prefixlen, &gw, iface, 0, metric);
 }
 
 static int
@@ -173,7 +174,7 @@
     }
     printf("\n");
 
-    return net_route_v6_add(NULL, &dst, prefixlen, NULL, iface, 0, metric);
+    return net_route_v6_add(ctx, &dst, prefixlen, NULL, iface, 0, metric);
 }
 
 static int
@@ -206,7 +207,7 @@
     }
     printf("\n");
 
-    return net_route_v6_add(NULL, &dst, prefixlen, &gw, iface, 0, metric);
+    return net_route_v6_add(ctx, &dst, prefixlen, &gw, iface, 0, metric);
 }
 
 static void
@@ -232,11 +233,19 @@
         return 0;
     }
 
-    if (argc > 3)
+    if (argc >= 3)
     {
         iface = argv[2];
     }
 
+    if (argc >= 4)
+    {
+        ctx = malloc(sizeof(*ctx));
+        CLEAR(*ctx);
+        ctx->netns = argv[3];
+        ctx->gc = gc_new();
+    }
+
     test = atoi(argv[1]);
     switch (test)
     {

--
To view, visit http://gerrit.openvpn.net/c/openvpn/+/1586?usp=email
To unsubscribe, or for help writing mail filters, visit http://gerrit.openvpn.net/settings?usp=email

Gerrit-MessageType: newpatchset
Gerrit-Project: openvpn
Gerrit-Branch: master
Gerrit-Change-Id: Ibaf84cba0124320f7c49e28d1b3364dac9e8b6d9
Gerrit-Change-Number: 1586
Gerrit-PatchSet: 2
Gerrit-Owner: flichtenheld <[email protected]...>
Gerrit-Reviewer: its_Giaan <[email protected]...>
Gerrit-Reviewer: plaisthos <[email protected]...>
Gerrit-CC: openvpn-devel <[email protected]>
Gerrit-Attention: plaisthos <[email protected]...>
Gerrit-Attention: its_Giaan <[email protected]...>
Gerrit-Attention: flichtenheld <[email protected]...>